import { createStore } from "vuex";
import { IRootState } from "./type";
import login from "./login/login";
import system from "./main/system/system";
import { getPageListData } from "@/service/main/system/system";
import dashboard from "./main/analysis/dashboard";

const store = createStore<IRootState>({
  state: () => {
    return {
      name: "codewhy",
      age: 18,
      entireDepartment: [],
      entireRole: [],
      entireMenu: [],
    };
  },
  actions: {
    async getInitialDataAction(context, value) {
      const departmentResult = await getPageListData("/department/list", {
        offset: 0,
        size: 100,
      });
      const { list: departmentList } = departmentResult.data;
      context.commit("changeEntireDepartment", departmentList);
      const roleResult = await getPageListData("/role/list", {
        offset: 0,
        size: 100,
      });
      const { list: roleList } = roleResult.data;
      context.commit("changeEntireRole", roleList);
      const menuResult = await getPageListData("/menu/list", {});
      const { list: menuList } = menuResult.data;
      context.commit("changeEntireMenu", menuList);
    },
  },
  mutations: {
    changeEntireDepartment(state, list) {
      state.entireDepartment = list;
    },
    changeEntireRole(state, list) {
      state.entireRole = list;
    },
    changeEntireMenu(state, list) {
      state.entireMenu = list;
    },
  },
  getters: {},
  modules: {
    login,
    system,
    dashboard,
  },
});
export function setupStore() {
  store.dispatch("login/loadLocalLogin");
  store.dispatch("getInitialDataAction");
}
export default store;
